package com.lm.scau.dyb.db.Dalx;

import android.content.ContentValues;
import android.database.Cursor;

import com.lm.scau.dyb.activity.base.BaseActivity;
import com.lm.scau.dyb.bean.Movie;
import com.lm.scau.dyb.bean.MovieRole;
import com.lm.scau.dyb.db.DybDB;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2016/8/20.
 */
public class MovieDalx extends BaseDalx {
    private static final String TB_NAME = "tb_movie";
    private static final String MV_ID = "mv_id";
    private static final String MV_NAME = "mv_name";
    private static final String MV_PHURL = "mv_phurl";
    private static final String MV_ROLE = "mv_role";
    private static final String MV_SUMMARY = "mv_summary";
    private static final String MV_TYPE = "mv_type";
    private static final String MV_URL = "mv_url";
    private static final String MV_TIME = "mv_time";
    private static final String MV_USING = "mv_using";


    public MovieDalx(BaseActivity activity) {
        if (db == null) {
            db = DybDB.get(activity);
        }
        if (!db.tabbleIsExist(TB_NAME)) {
            createDataFileTable();
        }
    }

    private void createDataFileTable() {
        String createTableSql = "CREATE TABLE IF NOT EXISTS " + TB_NAME + "(" + MV_ID
                + " int, " + MV_NAME + " varchar, " + MV_PHURL + " varchar, " + MV_ROLE + " varchar, " +
                MV_SUMMARY + " varchar, " + MV_TYPE + " varchar, " + MV_URL +
                " varchar, " + MV_TIME + " varchar, " + MV_USING + " varchar)";
        db.creatTable(createTableSql);
    }

    /**
     * 获取最新的轮播时间，用来更新最新数据
     *
     * @return
     */
    @Override
    public String getLatelyTime() {
        String sql = "select " + MV_TIME + " from " + TB_NAME +
                " order by " + MV_TIME + " desc limit ?";
        Cursor cursor = db.find(sql, new String[]{"1"});
        String time = "1970-01-01 00:00:00";

        while (cursor.moveToNext()) {
            time = cursor.getString(cursor.getColumnIndex(MV_TIME));
            break;
        }
        return time;
    }

    @Override
    public int getId() {
        String sql = "select " + MV_ID + " from " + TB_NAME +
                " order by " + MV_ID + " desc limit ?";
        Cursor cursor = db.find(sql, new String[]{"1"});
        int id = -1;
        while (cursor.moveToNext()) {
            id = cursor.getInt(cursor.getColumnIndex(MV_ID));
            break;
        }
        return id;
    }

    /**
     * 新增轮播数据
     *
     * @param movie
     * @return
     */
    public boolean addMovies(Movie movie) {
        boolean isOk = false;
        ContentValues values = new ContentValues();
        values.put(MV_ID, movie.getMoveiID());
        values.put(MV_NAME, movie.getMovieName());
        values.put(MV_PHURL, movie.getMoviePhurl());
        values.put(MV_ROLE, movie.getMovieRole());
        values.put(MV_SUMMARY, movie.getMovieSummary());
        values.put(MV_TIME, movie.getMovieTime());
        values.put(MV_TYPE, movie.getMovieType());
        values.put(MV_URL, movie.getMovieUrl());
        values.put(MV_USING, movie.getMoviceUsing());
        if (db.save(TB_NAME, values)) {
            isOk = true;
        }
        return isOk;
    }

    public List<Movie> getMovies(String role, String type,int rowId,int pagesize) {
        List<Movie> movies = new ArrayList<>();
        String sql = "select " + TB_NAME + ".* from tb_movie" +
                " where " + MV_TYPE + " like ? and " + MV_ROLE +
                " like ? and " + MV_ID + " > " + rowId +
                " order by " + MV_TIME + " limit " + pagesize;
        Cursor cursor = db.find(sql, new String[]{"%" + type + "%", "%" + role + "%"});
        while (cursor.moveToNext()) {
            Movie movie = new Movie();
            movie.setMoveiID(cursor.getInt(cursor.getColumnIndex(MV_ID)));
            movie.setMovieName(cursor.getString(cursor.getColumnIndex(MV_NAME)));
            movie.setMoviePhurl(cursor.getString(cursor.getColumnIndex(MV_PHURL)));
            movie.setMovieRole(cursor.getString(cursor.getColumnIndex(MV_ROLE)));
            movie.setMovieSummary(cursor.getString(cursor.getColumnIndex(MV_SUMMARY)));
            movie.setMovieTime(cursor.getString(cursor.getColumnIndex(MV_TIME)));
            movie.setMovieType(cursor.getString(cursor.getColumnIndex(MV_TYPE)));
            movie.setMovieUrl(cursor.getString(cursor.getColumnIndex(MV_URL)));
            movie.setMoviceUsing(cursor.getString(cursor.getColumnIndex(MV_USING)));
            movies.add(movie);
        }
        return movies;
    }
}
